//
//  BKMatchingScholarshipViewController.m
//  baokaodaxue
//
//  Created by SKar on 15/6/1.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKMatchingScholarshipViewController.h"
#import "BKSearchField.h"
#import "BKTextField.h"
#import "BKSearchPickViewDataSource.h"
#import "BKMatchingScholarshipViewModel.h"
#import "BKResultOfMatchingScholarshipViewController.h"
#import <MBProgressHUD.h>
@interface BKMatchingScholarshipViewController ()

@property (weak, nonatomic) IBOutlet BKSearchField *examLocationTextField;
@property (weak, nonatomic) IBOutlet BKTextField *scoreTextField;
@property (weak, nonatomic) IBOutlet BKTextField *rankBeginTextField;
@property (weak, nonatomic) IBOutlet BKTextField *rangEndTextField;
@property (weak, nonatomic) IBOutlet BKSearchField *majorTextField;
@property (weak, nonatomic) IBOutlet BKSearchField *piciTextField;
@property (weak, nonatomic) IBOutlet BKSearchField *schoolLocationTextField;
@property (weak, nonatomic) IBOutlet BKSearchField *typeTextField;
@property (weak, nonatomic) IBOutlet BKSearchField *jxjTextField;
@property (strong ,nonatomic) MBProgressHUD *hud;

@property (nonatomic, strong) BKSearchPickViewDataSource *dataSource;
@property (nonatomic, strong) BKMatchingScholarshipViewModel *viewModel;
 
@end

@implementation BKMatchingScholarshipViewController

- (instancetype)initWithNib {
    self = [super initWithNibName:NSStringFromClass([BKMatchingScholarshipViewController class]) bundle:nil];
    if (self) {
        _viewModel = [[BKMatchingScholarshipViewModel alloc] init];
        
    
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _majorTextField.dataArray = @[@"文科",@"理科"];
    _majorTextField.flag = 8011;
    _schoolLocationTextField.dataArray = @[
                                           @"不限",
                                           @"北京",
                                           @"天津",
                                           @"上海",
                                           @"重庆",
                                           @"河北",
                                           @"河南",
                                           @"山东",
                                           @"山西",
                                           @"安徽",
                                           @"江西",
                                           @"江苏",
                                           @"浙江",
                                           @"湖北",
                                           @"湖南",
                                           @"广东",
                                           @"广西",
                                           @"云南",
                                           @"贵州",
                                           @"四川",
                                           @"陕西",
                                           @"青海",
                                           @"宁夏",
                                           @"黑龙江",
                                           @"吉林",
                                           @"辽宁",
                                           @"西藏",
                                           @"新疆",
                                           @"内蒙古",
                                           @"海南",
                                           @"福建",
                                           @"甘肃"];
    _schoolLocationTextField.flag = 8011;
    
    _hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:_hud];
    _hud.mode = MBProgressHUDModeIndeterminate;
    
    
     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    if ([userDefaults objectForKey:@"kq"]) {
    
        _examLocationTextField.text = [userDefaults objectForKey:@"kq"];
    }
    if ([userDefaults objectForKey:@"kf"]) {
        _scoreTextField.text = [userDefaults objectForKey:@"kf"];
    }
    
    if ([userDefaults objectForKey:@"dq"]) {
        _schoolLocationTextField.text = [userDefaults objectForKey:@"dq"];
    }
    
    if ([userDefaults objectForKey:@"pc"]) {
        _piciTextField.text = [userDefaults objectForKey:@"pc"];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)submit:(id)sender {
    if (self.examLocationTextField.text.length == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择考区" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    
    if (self.scoreTextField.text.length == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择考分" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:_examLocationTextField.text forKey:@"kq"];
    [userDefaults setObject:_scoreTextField.text forKey:@"score"];
    
    
    
    if ([_schoolLocationTextField.text length]>0 ) {
         [userDefaults setObject:_schoolLocationTextField.text forKey:@"dq"];
    }
    
    if ([_piciTextField.text length]>0) {
        [userDefaults setObject:_piciTextField.text forKey:@"pc"];
    }

    
    [userDefaults synchronize];
    __weak BKMatchingScholarshipViewController *weakSelf = self;
    _hud.labelText = @"正在加载";
    [_hud show:YES];
    [self.viewModel colleageWithkq:self.examLocationTextField.text
                                kf:self.scoreTextField.text
                               pwb:self.rankBeginTextField.text
                               pwe:self.rangEndTextField.text
                                wl:self.majorTextField.text
                                pc:self.piciTextField.text
                                dp:self.schoolLocationTextField.text
                                fw:self.typeTextField.text
                                jz:self.jxjTextField.text
                       currentPage:@"1" success:^(NSArray *dataArray) {
                           
                           __strong BKMatchingScholarshipViewController *strongSelf = weakSelf;
                           
                           dispatch_async(dispatch_get_main_queue(), ^{
                               [weakSelf.hud hide:YES];
                               BKResultOfMatchingScholarshipViewController *vc = [[BKResultOfMatchingScholarshipViewController alloc] initWithNibByDataArray:dataArray];
                               vc.kf = strongSelf.scoreTextField.text;
                               vc.kq = strongSelf.examLocationTextField.text;
                               
                               [strongSelf.navigationController pushViewController:vc animated:YES];
                           });
                           
                       } failure:^(NSString *message) {
                           
                           dispatch_async(dispatch_get_main_queue(), ^{
                               [weakSelf.hud hide:YES];
                               UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                               [alertView show];
                           });
                           
                       }];
}
- (IBAction)p_back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
